package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.ProductionInstructionLinesDestinationSchemaEntity;
import cn.com.huzo.repository.trade.ProductionInstructionLinesDestinationSchemaRepository;
import cn.com.huzo.repository.trade.ProductionInstructionLinesRepository;
import cn.com.huzo.service.trade.ProductionInstructionLinesScheduleManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产指示书交付日期管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ProductionInstructionLinesScheduleManager")
public class ProductionInstructionLinesScheduleManagerImpl extends GenericManagerImpl<ProductionInstructionLinesDestinationSchemaEntity, String>
        implements ProductionInstructionLinesScheduleManager {

    @Resource(name = "ProductionInstructionLinesRepository")
    private ProductionInstructionLinesRepository _dao_lines;

    @Resource(name = "ProductionInstructionLinesDestinationSchemaRepository")
    private ProductionInstructionLinesDestinationSchemaRepository _dao_lines_schedule;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveSchedule(Map<String, Object> criterion) throws Exception {
        List<Map<String, Object>> _insertList = ((List<Map<String, Object>>) MapUtils.getObject(criterion, "InsertList"));
        List<Map<String, Object>> _updateList = ((List<Map<String, Object>>) MapUtils.getObject(criterion, "UpdateList"));
        // 新建
        if (!_insertList.isEmpty()) {
            for (int i = 0; i < _insertList.size(); i++) {
                _dao_lines_schedule.insertDeliveryDate(_insertList.get(i));
                writeAuditLog(MapUtils.getLong(_insertList.get(i), "UpdatedBy"), "贸易信息→生产指示书明细",
                        "[款号]" + MapUtils.getString(_insertList.get(i), "StyleNo") + " [交付日期]" + MapUtils.getString(_insertList.get(i), "NewScheduleDate"),
                        "增加");
            }
        }
        // 更新
        if (!_updateList.isEmpty()) {
            for (int i = 0; i < _updateList.size(); i++) {
                _dao_lines_schedule.updateDeliveryDate(_updateList.get(i));
                writeAuditLog(MapUtils.getLong(_updateList.get(i), "UpdatedBy"), "贸易信息→生产指示书明细",
                        "[款号]" + MapUtils.getString(_updateList.get(i), "StyleNo") + " [交付日期]" + MapUtils.getString(_updateList.get(i), "OldScheduleDate") + "==>" + MapUtils.getString(_updateList.get(i), "NewScheduleDate"),
                        "更新");
            }
        }

        Map<String, Object> _result = new HashMap();

        _result.put("ScheduleNode", _dao_lines_schedule.fetchScheduleDateNode(MapUtils.getLong(criterion, "HeaderId")));
        _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(MapUtils.getLong(criterion, "HeaderId"), null));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        _dao_lines_schedule.deleteDeliveryDate(criterion);
        // 审计日志
        writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→生产指示书明细",
                "[款号]" + MapUtils.getString(criterion, "StyleNo") + " [交付日期]" + MapUtils.getString(criterion, "ScheduleDate"),
                "删除");

        Map<String, Object> _result = new HashMap();

        _result.put("ScheduleNode", _dao_lines_schedule.fetchScheduleDateNode(MapUtils.getLong(criterion, "HeaderId")));
        _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(MapUtils.getLong(criterion, "HeaderId"), null));
        _result.put("SumQuantity", _dao_lines.fetchTotalQuantityByHeaderId(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }
}
